package minio.demo;

import com.alibaba.fastjson.JSON;
import io.minio.*;
import io.minio.errors.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class MinIODemo {

    private static final String accessKey = "PPeCeQbHtFsEcGNxnlmSpbYasxELHoGy";
    private static final String secretKey = "GEMQDMUhQSHaIJCqmxperpESNYCJXvjJ";
    private static final String endpoint = "http://192.168.8.88:9000";


    public static void main(String[] args)
            throws ServerException, InsufficientDataException, ErrorResponseException,
            IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String bucket = "test01";
        String objectName = "my_img01.jpg";
        downloadFile(bucket, objectName);
        String fileName = "C:\\Users\\pc\\Desktop\\11.jpg";
        //uploadFile(bucket, objectName, fileName);
    }

    public static void downloadFile(String bucket, String objectName)
            throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint(endpoint)
                        .credentials(accessKey, secretKey)
                        .build();
        try (InputStream inputStream = minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(bucket)
                        .object(objectName)
                        .build())) {
            // Read data from stream
            byte[] bytes = IOUtils.toByteArray(inputStream);
            String fileName = "22.jpg";
            System.out.println("下载文件：" + bytes.length);
            FileUtils.writeByteArrayToFile(new File("C:\\Users\\pc\\Desktop\\" + fileName), bytes);
            System.out.println("文件下载成功");
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static void uploadFile(String bucket, String objectName, String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(endpoint)
                            .credentials(accessKey, secretKey)
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
            if (!found) {
                // Make a new bucket called bucket.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
            } else {
                System.out.println("Bucket " + bucket + " already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucket)
                            .object(objectName)
                            .filename(fileName)
                            .build());

            //partSize 为-1自动设置
//            MultipartFile file = new CommonsMultipartFile(null);
//            minioClient.putObject(PutObjectArgs.builder()
//                    .bucket(bucket).object(objectName).stream(file.getInputStream(), file.getSize(), -1)
//                    .contentType(file.getContentType()).build());

            System.out.println(
                    fileName + " is successfully uploaded as"
                            + objectName + " to bucket " + bucket + ".");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }
    }

}
